package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.base.BaseController;
import com.ssm.entity.House;
import com.ssm.entity.HouseImage;
import com.ssm.result.Result;
import com.ssm.service.HouseImageService;
import com.ssm.service.HouseService;
import com.ssm.util.QiniuUtils;
import com.ssm.vo.HouseVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author jie_ge
 * @Date 2022/4/19
 */
@Controller
@RequestMapping("/houseImage")
public class HouseImageController extends BaseController {
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseService houseService;
    /**
     * 上传图片界面
     * @param map
     * @param houseId
     * @param type
     * @return
     */
    @GetMapping("/uploadShow/{houseId}/{type}")
    public String uploadShow(Map map, @PathVariable("houseId") Long houseId,@PathVariable("type") Long type) {
        map.put("houseId",houseId);
        map.put("type",type);
        return "house/upload";
    }

    /**
     * 保存添加的图片
     * @param files
     * @param houseId
     * @param type
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload/{houseId}/{type}")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile[] files, @PathVariable("houseId") Long houseId, @PathVariable("type") Integer type) throws IOException {
        if(files.length > 0){
            for (MultipartFile file : files) {
                String newFileName = UUID.randomUUID().toString();
                QiniuUtils.upload2Qiniu(file.getBytes(),newFileName);
                String url = "http://ral7nlzq1.hn-bkt.clouddn.com/"+newFileName;

                House house = new House();

                house.setDefaultImageUrl(url);
                houseService.update(house);


                HouseImage houseImage = new HouseImage();
                houseImage.setHouseId(houseId);
                houseImage.setType(type);
                houseImage.setImageName(file.getOriginalFilename());
                houseImage.setImageUrl(url);
                houseImageService.insert(houseImage);
            }
        }
        return Result.ok();
    }

    /**
     * 删除图片
     * @param map
     * @param houseId
     * @param id
     * @param redirectAttributes
     * @return
     */
    @GetMapping("/delete/{houseId}/{type}")
    public String delete(Map map, @PathVariable("houseId") Long houseId, @PathVariable("type") Long id, RedirectAttributes redirectAttributes) {
        HouseImage houseImage = houseImageService.getById(id);
        houseImageService.deleteById(id);
        //从七牛云中删除图片
        QiniuUtils.deleteFileFromQiniu(houseImage.getImageUrl());
        return "redirect:/house/"+houseId;
    }
}
